from rest_framework.viewsets import ModelViewSet
from rest_framework.response import Response
from orders.models import Orders, OrdersProduct
from orders.serializers import OrdersSerializer, OrdersProductSerializer
from libs.utils.kuaidi import get_kuaidi_info, KUAIDI_STATES



class OrdersView(ModelViewSet):
    queryset = Orders.objects.all()
    serializer_class = OrdersSerializer

    # 使用filter过滤
    my_filter_fields = ('status', 'pay_status', 'order_number', 'logistics_name',
                        'logistics_mobile', 'official', 'mobile', 'time_type')

    ## 获取筛选参数
    def get_kwargs_for_filtering(self):
        filtering_kwargs = {}
        for field in self.my_filter_fields:
            field_value = self.request.query_params.get(field)
            if field_value:
                if field == "time_type":
                    startTime = self.request.query_params.get("start_time")
                    stopTime = self.request.query_params.get("stop_time")
                    if field_value == "1":
                        filtering_kwargs["ctime__gte"] = startTime
                        filtering_kwargs["ctime__lte"] = stopTime
                    elif field_value == "2":
                        filtering_kwargs["logistics_time__gte"] = startTime
                        filtering_kwargs["logistics_time__lte"] = stopTime
                    else:
                        filtering_kwargs["confirm_time__gte"] = startTime
                        filtering_kwargs["confirm_time__lte"] = stopTime

                elif field == 'logistics_name' or field == 'logistics_mobile' or field == 'mobile':
                    newField = field + "__contains"
                    filtering_kwargs[newField] = field_value
                else:
                    filtering_kwargs[field] = field_value
        return filtering_kwargs

    def list(self, request):
        """
        商品列表
        """
        filtering_kwargs = self.get_kwargs_for_filtering()

        if filtering_kwargs:
            self.queryset = self.queryset.filter(**filtering_kwargs)

        pagination_queryset = self.paginate_queryset(self.queryset)
        serializer = self.get_serializer(pagination_queryset, many=True)
        resp = self.get_paginated_response(serializer.data)

        return resp

    def retrieve(self, request, *args, **kwargs):
        obj = self.get_object()
        serializer = self.get_serializer(obj)
        orders = serializer.data

        # 获取订单物流信息
        logistics = {}
        if orders.logistics_number:
            kudiInfo = get_kuaidi_info(orders.logistics_company_code, orders.logistics_number, orders.logistics_mobile)
            if kudiInfo["status"] == "200":
                logistics = {
                    "message": kudiInfo["message"],
                    "nu": kudiInfo["nu"],
                    "state": KUAIDI_STATES[kudiInfo["state"]],
                    "status": kudiInfo["status"],
                    "com": orders.logistics_company,
                    "data": kudiInfo["data"]
                }
            else:
                logistics = {
                    "status": kudiInfo["returnCode"],
                    "message": kudiInfo["message"]
                }

        orders.logistics = logistics

        return Response(orders)
